#include <list>
#include <iostream>

void josephus(int n, int k) {
    // 创建一个链表，代表环
    std::list<int> people;
    for (int i = 1; i <= n; ++i) 
        people.push_back(i);

    auto it = people.begin();              // 迭代器指向链表的开始
    while (people.size() > 2) {
        for (int stride = 1; stride < k; ++stride) {
            ++it;
            if (it == people.end()) {      // 如果迭代器到达了链表的末尾，就回到开始
                it = people.begin();
            }
        }
        
        it = people.erase(it);            // 移除当前迭代器指向的元素，并将迭代器移到下一个元素
        if (it == people.end()) {         // 如果迭代器到达了链表的末尾，就回到开始
            it = people.begin();
        }
    }

    std::cout << "The two last: " << people.front() << " " << people.back() << std::endl;
}

int main() {
    int n, k = 3; // 总人数n，每次跳过的人数k
    std::cout << "Enter the total number of people (n): ";
    // std::cin >> n; // 用户输入总人数
    n = 34;

    josephus(n, k); // 调用函数求解约瑟夫环问题

    return 0;
}